<?php

namespace app\order\logic;

use app\data\util\HttpUtil;
use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;

class OrderExchangeLogic
{
    protected $model = null;
    public function __construct()
    {
        $this->model = new \app\order\model\OrderExchange();
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $limit = $params['limit'] ?? 10;
        $where = [];
        $date  = $params['date'] ?? '';



        if($date){
            $where[] = ['date','=',current($date)];
        }

        $list = Db::name('order_exchange')
            ->where($where)
            ->order("date desc")
            ->paginate($limit);

        $lists = $list->items();
                
        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $lists = [];
        $header = [];
        // $header[] = ['广告账户', 'advertiser_id'];
        $fileName = "export_Advaccount" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }

    public function add($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $result = false;

        try {
            //业务

            if ($result !== false) {
                return json(DefaultResponse(200, true, '添加成功!', null));
            } else {
                return json(DefaultResponse(201, false, '添加失败!', null));
            }
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '广告账户已存在!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));

    }

    public function find($id)
    {
        $where['id'] = $id;
        $row = Db::name('order_exchange')
            ->where($where)
            ->find();
        
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        return json(DefaultResponse(200, true, '获取成功', $row));
    }

    public function edit($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $id = $data['id'];
        $row = $this->model->find($id);
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        $result = false;

        try {
            
            //广告账户
            $OrderExchange['dollar'] = $data['dollar'] ?? 0;
            $OrderExchange['euro_exchange_rate'] = $data['euro_exchange_rate'] ?? 0;
            $OrderExchange['gbp_exchange_rate'] = $data['gbp_exchange_rate'] ?? 0;
            $OrderExchange['mexicanpeso'] = $data['mexicanpeso'] ?? 0;
            $OrderExchange['LogisticsMarkupFee'] = $data['LogisticsMarkupFee'] ?? 0;
            $OrderExchange['operate_time'] = date('Y-m-d H:i:s',time());
            $OrderExchange['single_price'] =  $data['single_price'] ?? 0;
            $OrderExchange['register_fee'] =  $data['register_fee'] ?? 0;

            $result = $row->save($OrderExchange);

            if ($result) {
                return json(DefaultResponse(200, false, '更新成功!', null));
            }
            return json(DefaultResponse(201, false, '更新失败!', null));
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '更新失败!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));
    }
    
    
}
